package com.ayitubie.refuelweb.controller;

import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

public class SzsePdfDownloader {

    private static final String BASE_URL = "https://www.szse.cn/api/search/content";
    private static final String DOWNLOAD_DIR = "downloads";
    private static final int PAGE_SIZE = 20;
    private static final Random random = new Random();
    private static final RestTemplate restTemplate = new RestTemplate();
    private static final ObjectMapper objectMapper = new ObjectMapper();

    // 用于移除HTML标签的正则表达式
    private static final Pattern HTML_TAG_PATTERN = Pattern.compile("<[^>]*>");

    public static void main(String[] args) {
        try {
            // 创建下载目录
            Path dirPath = Paths.get(DOWNLOAD_DIR);
            if (!Files.exists(dirPath)) {
                Files.createDirectories(dirPath);
            }

            // 首次请求获取总条数
            JsonNode firstPage = sendRequest(1);
            int totalSize = firstPage.path("totalSize").asInt();
            System.out.println("总共发现 " + totalSize + " 条记录");

            // 计算总页数
            int totalPages = (int) Math.ceil((double) totalSize / PAGE_SIZE);
            System.out.println("需要处理 " + totalPages + " 页");

            // 处理第一页
            processPage(firstPage);

//            // 处理剩余页
//            for (int currentPage = 2; currentPage <= totalPages; currentPage++) {
//                // 随机延迟，防止被封
//                TimeUnit.MILLISECONDS.sleep(500 + random.nextInt(1000));
//
//                JsonNode response = sendRequest(currentPage);
//                processPage(response);
//                System.out.println("已处理第 " + currentPage + " 页/" + totalPages);
//            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static JsonNode sendRequest(int currentPage) throws IOException {
        // 构建请求参数
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("random", String.valueOf(Math.random()));
        params.add("keyword", "财报");
        params.add("orderby", "score");
        params.add("time", "1");
        params.add("currentPage", String.valueOf(currentPage));
        params.add("pageSize", String.valueOf(PAGE_SIZE));
        params.add("searchtype", "1");
        params.add("openChange", "true");
        params.add("range", "title");

        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36");

        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);

        // 发送POST请求
        ResponseEntity<String> response = restTemplate.exchange(
                BASE_URL,
                HttpMethod.POST,
                requestEntity,
                String.class);

        // 解析JSON响应
        return objectMapper.readTree(response.getBody());
    }

    private static void processPage(JsonNode pageData) {
        JsonNode docs = pageData.path("data");
        for (JsonNode doc : docs) {
            String docpubjsonurl = doc.path("docpubjsonurl").asText();
            if (docpubjsonurl.toLowerCase().endsWith(".pdf") || docpubjsonurl.toLowerCase().endsWith(".docx")) {
                try {
                    // 获取并清理文件名
                    String originalTitle = doc.path("doctitle").asText();
                    String cleanFileName = "";
                    if (docpubjsonurl.toLowerCase().endsWith(".pdf")){
                        cleanFileName = cleanFileName(originalTitle) + ".pdf";
                    }else{
                        cleanFileName = cleanFileName(originalTitle) + ".docx";
                    }
                    cleanFileName = cleanFileName(originalTitle) + ".pdf";
                    downloadPdf(docpubjsonurl, cleanFileName);
                } catch (Exception e) {
                    System.err.println("下载PDF失败: " + docpubjsonurl + " - " + e.getMessage());
                }
            }
        }
    }

    /**
     * 清理文件名中的HTML标签和非法字符
     */
    private static String cleanFileName(String originalTitle) {
        // 1. 移除HTML标签
        String cleanName = HTML_TAG_PATTERN.matcher(originalTitle).replaceAll("");

        // 2. 移除前后空格
        cleanName = cleanName.trim();

        // 3. 替换文件系统不允许的字符
        cleanName = cleanName.replaceAll("[\\\\/:*?\"<>|]", "_");

        // 4. 限制文件名长度（Windows最大255字符，我们取100）
        return cleanName.length() > 100 ? cleanName.substring(0, 100) : cleanName;
    }

    private static void downloadPdf(String pdfUrl, String fileName) throws IOException {
        Path filePath = Paths.get(DOWNLOAD_DIR, fileName);

        // 检查文件是否已存在
        if (Files.exists(filePath)) {
            System.out.println("文件已存在，跳过下载: " + fileName);
            return;
        }

        System.out.println("正在下载: " + fileName + " (原始URL: " + pdfUrl + ")");

        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.set("User-Agent", "Mozilla/5.0");
        HttpEntity<String> entity = new HttpEntity<>(headers);

        // 获取文件流
        ResponseEntity<byte[]> response = restTemplate.exchange(
                pdfUrl,
                HttpMethod.GET,
                entity,
                byte[].class);

        // 保存文件
        try (FileOutputStream fos = new FileOutputStream(filePath.toFile())) {
            fos.write(response.getBody());
            System.out.println("下载完成: " + fileName);
        }
    }
}
